package win.acfelis.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import win.acfelis.domain.DetailModel;
import win.acfelis.domain.UserInfo;
import win.acfelis.service.AdverService;
import win.acfelis.service.UserService;
import win.acfelis.tools.ApiResult;
import win.acfelis.tools.AppContent;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

/**
 * 用户管理
 * 刘柱
 * 20181230 1825
 */
@Controller
@RequestMapping("admin")
public class SettingController {

    @Resource
    private UserService userService;

    @Resource
    private AdverService adverService;

    /**
     * 跳转登录页面
     *
     * @return 登录页面地址
     */
    @RequestMapping({"/", "", "/login"})
    public String loginJsp() {
        return "login/login";
    }

    /**
     * 登录操作
     *
     * @param userInfo
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "login", method = POST)
    public ApiResult login(UserInfo userInfo, HttpServletRequest req) {
        if (StringUtils.isEmpty(userInfo.getUserName()) ||
                StringUtils.isEmpty(userInfo.getPassword())) {
            return new ApiResult().failure(AppContent.LOGIN_USERNAME_ISBANK, "登录失败，账号密码不能为空！");
        }
        ApiResult result = userService.login(userInfo);
        if (!result.isSucc()) {
            return result;
        }
        HttpSession session = req.getSession();
        session.setAttribute("userName", userInfo.getUserName());
        session.setMaxInactiveInterval(30 * 60);

        return result;
    }

    /**
     * 管理页面跳转
     *
     * @return 管理首页
     */
    @RequestMapping("manager")
    public String mangerJsp() {
        return "manager/telIndex";
    }

    /**
     * 列表页面跳转
     *
     * @return 列表页面
     */
    @RequestMapping(value = "list", method = {GET, POST})
    public String listJsp(Model model, String keyword) {
        model.addAttribute("list", adverService.list(keyword));
        model.addAttribute("keyword", keyword);
        return "manager/detail/list";
    }

    /**
     * 详情页面跳转
     *
     * @param model 回传参数
     * @return 如果有guid 返回详情页，如果没guid 返回新建页面
     */
    @RequestMapping(value = "detail", method = {GET ,POST})
    public String detailJsp(Model model, HttpServletRequest req) {
        String guid = req.getParameter("guid");
        if (!StringUtils.isEmpty(guid)) {
            model.addAttribute("detail", adverService.detail(guid));
        }
        return "manager/detail/detail";
    }

    /**
     * 新增或者修改信息
     *
     * @param info 信息对象
     * @return 新增或者修改结果
     */
    @ResponseBody
    @RequestMapping(value = "add", method = POST)
    public ApiResult add(DetailModel info) {
        return adverService.add(info);
    }

}
